package com.winbomb.kingcard.base;

import java.util.ArrayList;
import java.util.List;

import com.winbomb.kingcard.IAttackRoute;
import com.winbomb.kingcard.IGameLogic;
import com.winbomb.kingcard.IPlayer;
import com.winbomb.kingcard.base.anim.IPlayable;
import com.winbomb.kingcard.base.anim.impl.ActionAnim;
import com.winbomb.kingcard.base.anim.impl.AttackAnim;
import com.winbomb.kingcard.base.anim.impl.BattleAnim;
import com.winbomb.kingcard.base.anim.impl.BattleResultAnim;
import com.winbomb.kingcard.base.anim.impl.BattleStartAnim;
import com.winbomb.kingcard.base.anim.impl.DealingCardAnim;
import com.winbomb.kingcard.base.anim.impl.MarchAnim;
import com.winbomb.kingcard.base.anim.impl.MovingCardsAnim;
import com.winbomb.kingcard.base.anim.impl.WinLoseAnim;

public class AnimManager {

	public static MovingCardsAnim MOVING_CARDS_ANIM;
	public static BattleAnim BATTLE_ANIM;
	public static MarchAnim MARCH_ANIM;
	public static DealingCardAnim DEALING_CARD_ANIM;
	public static AttackAnim ATTACK_ANIM;
	public static BattleResultAnim BATTLE_RESULT_ANIM;
	public static ActionAnim ACTION_ANIM;

	public static final int MAX_ANIM_COUNT = 5;

	public static final int ANIM_DEALING_CARD = 1;
	public static final int ANIM_MARCH = 2;
	public static final int ANIM_BATTLE = 3;
	public static final int ANIM_ATTACK = 4;
	public static final int ANIM_BATTLE_LOSS = 5;
	public static final int ANIM_WIN_LOSE = 6;
	public static final int ANIM_BATTLE_START = 7;

	private IGameLogic game;

	/** 是否播放完毕 */
	private boolean finishPlaying;

	/** 待播放的动画数组 */
	private List<IPlayable> playList;

	public AnimManager(IGameLogic game) {
		this.game = game;
		this.finishPlaying = true;
		this.playList = new ArrayList<IPlayable>();

		MOVING_CARDS_ANIM = new MovingCardsAnim(game);
		BATTLE_ANIM = new BattleAnim(game);
		MARCH_ANIM = new MarchAnim(game);
		DEALING_CARD_ANIM = new DealingCardAnim(game);
		ATTACK_ANIM = new AttackAnim(game);
		BATTLE_RESULT_ANIM = new BattleResultAnim(game);
		ACTION_ANIM = new ActionAnim(game);
	}

	public boolean isFinishPlaying() {
		return finishPlaying;
	}

	public void addAnimation(IPlayable anim) {
		if (playList.size() >= MAX_ANIM_COUNT) {
			return;
		}

		playList.add(anim);
		finishPlaying = false;
		game.setPause(true);
	}

	public void play(float dt) {

		if (playList.size() == 0) {
			finishPlaying = true;
			game.setPause(false);
			return;
		}

		if (playList.get(0).play(dt)) {
			playList.remove(0);
		}

	}

	public void addAnimation(int animId) {
		switch (animId) {
		case ANIM_DEALING_CARD:
			DealingCardAnim dealAnim = DEALING_CARD_ANIM;
			dealAnim.reset();
			dealAnim.setTarget(game.getCurrPlayer());
			addAnimation(dealAnim);
			break;

		case ANIM_MARCH:
			IAttackRoute atkRoute = game.getCurrTurn().getAttackRoute();
			IPlayer currLoc = atkRoute.getCurrLocation();
			IPlayer currTarget = atkRoute.getCurrTarget();
			MarchAnim marchAnim = MARCH_ANIM;
			marchAnim.reset();
			marchAnim.newMarch(currLoc, currTarget);
			addAnimation(marchAnim);
			break;

		case ANIM_BATTLE:
			BattleAnim battleAnim = BATTLE_ANIM;
			IPlayer targetPlayer = game.getBattle().getGuarder();
			battleAnim.reset();
			battleAnim.setTargetPos(targetPlayer.getPlayerInfo().getFramePos());
			addAnimation(battleAnim);
			game.setPause(false);
			break;

		case ANIM_WIN_LOSE:
			boolean isUpWin = (!game.getBattle().getWinner().isHuman());
			WinLoseAnim winLoseAnim = new WinLoseAnim(game, isUpWin);
			addAnimation(winLoseAnim);
			break;

		case ANIM_BATTLE_START:
			BattleStartAnim battleStartAnim = new BattleStartAnim(game);
			addAnimation(battleStartAnim);
			break;

		case ANIM_BATTLE_LOSS:

		}
	}
}
